package com.cxf.controller;

import com.cxf.pojo.Order;
import com.cxf.service.OrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/OrderCenter")
public class OrderCenter {
	
	@RequestMapping("/orderCenter")
	public ModelAndView orderCenter(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		//String userName = (String)request.getSession().getAttribute("userName");
		String userName = (String)request.getSession().getAttribute("userName");
		OrderService orderService = (OrderService)ctx.getBean("orderServiceImpl");
		List<com.cxf.pojo.Order> orderList = orderService.getOrderByUserName(userName);
		mv.addObject("url",request.getSession().getAttribute("url"));
		mv.addObject("orderList",orderList);
		mv.setViewName("orderCenter");
		return mv;
	}

    /**
     * 取消订单
     */
	@RequestMapping("/cancelOrder")
	public void cancelOrder(HttpServletRequest request,HttpServletResponse response) {
        response.setContentType("application/json");
	    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        OrderService orderService = (OrderService)ctx.getBean("orderServiceImpl");
        //获取编号
        String id = request.getParameter("orderId");
        Integer orderId = Integer.parseInt(id);
        //取消订单
        orderService.cancelOrder(orderId);
        System.out.println("取消成功");
        PrintWriter printWriter = null;
        try {
            printWriter = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        printWriter.write("{\"code\":\"1\"}");
        printWriter.close();
    }

}
